<?php
/**
 * Created by PhpStorm.
 * User: teddy
 * Date: 15-12-22
 * Time: 下午3:23
 */

/*载入实体类*/
include_once "User.php";

/*对PHP的流概念要好好理解一下，如果只post一个json数据，则需要把它变成流的形式来传给后台的php，然后再进行封装成json对象来操作*/
/*$_POST这种形式是获取不到数据的;*/
$raw_post_data = file_get_contents('php://input', 'r');

/*取到POST的数据，然后进行urlencode，以防止里面有中文*/
$jsonResultEnCode = urlencode($raw_post_data);

/*把传进来的String转成了Json对象*/
$jsonEntity = json_decode($raw_post_data);

/*urldecode输出，输出的为String*/
$jsonResult = urldecode(json_encode($jsonResultEnCode));

$user = new User();
$user->status = 1;
$user->name = $jsonEntity->userName;
$user->address = 'TaiWan';
$user->tel = 123456;
$user->pwd = '123456';

/*echo var_dump($user);

echo var_dump($jsonEntity);

echo var_dump($jsonResult);*/

if(addUser($user) >0 )
{
    echo "Success";
}
else
{
    echo "Fail";
}

function addUser($user)
{
    $mysqli = new mysqli('localhost','root','1','DTXMB');
    if (mysqli_connect_errno())
    {
        echo "{" . '"error"' . ":" . mysqli_connect_error() . "}";
    }
    $sql = "INSERT INTO `DTXMB`.`Custom` (`name`,`pwd`,`status`,`address`,`tel`) VALUES ('" . $user->name . "','" . $user->pwd . "','" . $user->status . "','" . $user->address . "','" . $user->tel . "')";
    echo $sql;
    $mysqli->query("set character set 'utf8'");
    $mysqli->query("set names 'utf8'");
    $ret = $mysqli->query($sql);
    $rc = $mysqli->affected_rows;
    return $rc;
}